<template>
  <div class="app-container">
    <el-card>
      <div slot="header" class="clearfix">
        <span>调查问卷答题记录</span>
      </div>

      <!-- 查询条件 -->
      <el-form :inline="true" :model="queryParams" class="demo-form-inline">
        <el-form-item label="用户ID">
          <el-input v-model="queryParams.userId" placeholder="请输入用户ID" clearable />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleQuery">查询</el-button>
          <el-button type="warning" icon="el-icon-refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 答题列表 -->
      <el-table :data="answerList" border style="width: 100%">
        <el-table-column prop="id" label="ID" width="60"/>
        <el-table-column prop="userId" label="用户ID" width="80"/>
        <el-table-column prop="questionId" label="问题ID" width="100"/>
        <el-table-column prop="answerValue" label="答案" />
        <el-table-column prop="roundNum" label="轮次" width="80"/>
        <el-table-column prop="createTime" label="创建时间" width="180"/>
      </el-table>

      <!-- 分页 -->
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>
  </div>
</template>

<script>
import { getAnswerList, getUserAnswers } from "@/api/system/surveyAnswer";

export default {
  name: "SurveyAnswer",
  data() {
    return {
      answerList: [],
      total: 0,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询答题列表 */
    getList() {
      const currentUserId = this.$store.state.user.userId; // 从 Vuex 拿当前登录ID
      if (currentUserId === 1) {
        // 管理员 → 查所有（不传 userId）
        getAnswerList(this.queryParams).then((res) => {
          this.answerList = res.rows;
          this.total = res.total;
        });
      } else {
        // 普通用户 → 只能查自己
        getUserAnswers(currentUserId).then((res) => {
          this.answerList = res.rows;
          this.total = res.total;
        });
      }
    },
    /** 搜索 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置 */
    resetQuery() {
      this.queryParams = { pageNum: 1, pageSize: 10, userId: null };
      this.getList();
    },
  },
};
</script>
